<?php
class fankui extends base{

	public function init(){ // 公用
		parent::init(); // 这是必须的

		$this->tit = "反馈";
		$this->current = 'fankui';

		$this->fankuiObj = D('company_fankui');
		$this->fankui_logObj = D('company_fankui_log');

		$this->areaObj = D('company_area');
		$this->cateObj = D('company_cate');
		$this->zoneObj = D('company_zone');
		$this->bingchuangObj = D('company_bingchuang');

		$this->deviceObj = D('company_device');
		$this->adminObj = D('company_admin');
		$this->userObj = D('company_user');
		$this->diaochaObj = D('company_diaocha');

		// $this->smsObj = D('sms');

		$this->auth_list = $this->adminObj->get_auth_list($this->admin);
		$this->areas = $this->auth_list['areas'];
		$this->cates = $this->auth_list['cates'];
		$this->zones = $this->auth_list['zones'];
		$this->bingchuangs = $this->auth_list['bingchuangs'];
		// dump($this->auth_list);exit;

		$this->config = $this->company_setting['baoxiu'];
		// dump($this->config);exit;

		$cond = "status=1 ".$this->cond_base;
		$this->admin_alls = $this->adminObj->fetchAll(array($cond),'id asc');

	}


	public function index(){
		$this->tab = $this->tab?$this->tab:'1';
		$arrBase = array('tab' => $this->tab,'page' => '0000',);
		if($this->keyword){
			$arrBase = array_merge($arrBase,array('keyword' => $this->keyword));
		}
		$this->nextpage = url($this->routeUrl,'loaddata', $arrBase);

	}


	//loaddata
	public function loaddata(){

		$cond = "status=1 ".$this->cond_base;
		// 加入查询条件
		if($this->tab){
			switch($this->tab){
				case '8':
					$cond .=" and order_status=99";
					break;
				default:
					$cond .=" and order_status={$this->tab}";
			}
		}
		//权限
		if($this->admin){
			if($this->admin['is_admin2']){
				$cond .=" ";
			}else{
				switch($this->admin['level']){
					case '0'://管理员
						$cond .=" ";
						break;
					case '1'://报修员
						$cond .=" and admin_id={$this->admin_id}";
						break;
					case '2'://报修组长
						$cond .=" ".$this->cond_diaocha.$this->cond_zone;
						break;
					case '3'://维修师傅
						// $cond .=" and shifu_id={$this->admin_id}";
						$cond .= " and ( shifu_id={$this->admin_id} or find_in_set('{$this->admin_id}',admin_ids) )";
						break;
					case '4'://维修组长
						$cond .=" ".$this->cond_diaocha;
						break;
					case '5'://护士长   看到科室全部
						// $cond .=" ".$this->cond_zone;
						$cond .= " and ( shifu_id={$this->admin_id} or find_in_set('{$this->admin_id}',admin_ids) )";
						break;
					case '6'://主任   看到科室全部
						// $cond .=" ".$this->cond_zone;
						$cond .= " and ( shifu_id={$this->admin_id} or find_in_set('{$this->admin_id}',admin_ids) )";
						break;
				}
			}
		}else{
			$cond .=" and user_id={$this->admin_id}";
		}

		//搜索
		if($this->keyword){
			$cond.=" and (title like '%".$this->keyword."%' or intro like '%".$this->keyword."%' and local like '%".$this->keyword."%') ";
		}
		// dump($this->admin);exit;
		$result = $this->fankuiObj->getList($cond,1);
        if($result){
			$lists = $result['list'];
			// foreach ($lists as $val) {

            //     if($val['admin_ids']){
            //         $val['admin_ids'] = explode(',',$val['admin_ids']);
            //     }
            //     $list[] = $val;
            // }
			//
        	// foreach ($lists as $val) {
			// 	if($val['photo']){
			// 		$photos = explode(',', $val['photo']);
			// 		$val['img'] = $photos[0];
			// 	}
			// 	$list[] = $val;
			// }
        	// $this->list = $list;
			// $this->showPage = $result['showPage'];
			// $this->total = $result['total'];

			$this->list = $lists;

			$this->areas = $this->areaObj->ids($lists,'area_id');
			$this->cates = $this->cateObj->ids($lists,'cate_id');
			$this->zones = $this->zoneObj->ids($lists,'zone_id');
			$this->bingchuangs = $this->bingchuangObj->ids($lists,'bingchuang_id');
			$this->devices = $this->deviceObj->ids($lists,'device_id');

			$this->users = $this->userObj->ids($lists,'user_id');
			$this->admins = $this->adminObj->ids($lists,'admin_id');
			$this->shifus = $this->adminObj->ids($lists,'shifu_id');
		}else{
			die('0');
		}

	}


	public function search(){
		// $this->cate = $this->cateObj->find(array("url='{$url}' and status=1"));
	}


	public function add(){

		if($this->fmdo == 'add'){

			$data['area_id'] = arg('area_id');
        	$data['cate_id'] = arg('cate_id');
        	$data['zone_id'] = arg('zone_id');
			$data['company_id'] = $this->company['id'];

			$shifu_id = $this->adminObj->get_shifu_id($data,$this->config);

			$order_status = 1;
			if($this->config['bx_moshi']>1){
				$order_status = 2;
				$up_time = date('Y-m-d H:i:s');
			}

			$device_id = arg('device_id') ?? 0;
			if($device_id){
				$device = $this->deviceObj->findById($data['device_id']);
				// $data['zone_id'] = $device['zone_id'];
			}

			$zone_id = arg('zone_id') ?? 0;
			if($zone_id){
				$zone = $this->zoneObj->findById($data['zone_id']);
				// $data['zone_id'] = $device['zone_id'];
				$zone_id = $zone['id'];
			}

			$area_id = arg('area_id') ?? 0;

			$arrBase = array(
				'company_id' => $this->company['id'],
				'admin_id' => $this->admin_id,
				'shifu_id' => $shifu_id,
				'title' => arg('title'),
				'area_id' => $area_id,
				'cate_id' => arg('cate_id'),
				'zone_id' => $zone_id,
				'device_id' => $device_id,
				'local' => arg('local'),
				'intro' => arg('intro'),
				'photo' => joinStr(arg('photo')),
				'order_status' => $order_status,
				'add_time' => date('Y-m-d H:i:s'),
				'up_time' => $up_time,
			);

			$order_id = $this->orderObj->add($arrBase);
			if($order_id){

				//log
				$data['order_status'] = $order_status;
				$this->order_logObj->add_log($order_id,$data);

				//发送短信
				$this->orderObj->sendsms($order_id,$order_status,$this->config);
			}

			$this->renderSuccess('提交成功',"/{$this->routeUrl}/index?tab=".$order_status);
		}

		//扫码
		//
		$this->device_id = arg('device_id');
		if($this->device_id){
			$this->device = $this->deviceObj->get_detail($this->device_id);
			$this->display($this->routeUrl."/device_set.php");
			exit;
		}

		$this->zone_id = arg('zone_id');
		if($this->zone_id){
			$this->zone = $this->zoneObj->findById($this->zone_id);
			$this->display($this->routeUrl."/zone_set.php");
			exit;
		}

		$this->info = array('status'=>1);

		$tpl = 'set.php';
		if($this->admin['level']==10){
			$tpl = 'set10.php';
		}
		$this->display($this->routeUrl."/".$tpl);

	}

	//详情
	public function detail(){
		$id = arg('id');
		$info = $this->fankuiObj->get_detail($id);
		if($info){
			$this->info = $info;

			if($info['pj_star1']){
				for ($i=1; $i <= $info['pj_star1']; $i++) {
					$pj_star1[$i] = 'checked';
				}
				for ($i=1; $i <= $info['pj_star2']; $i++) {
					$pj_star2[$i] = 'checked';
				}
				$this->pj_star1 = $pj_star1;
				$this->pj_star2 = $pj_star2;
			}
		}
	}

	//操作订单
	public function order_act(){
		$order_status = arg('order_status');
		$order_id = arg('id');
		if($this->fmdo == 'order_act'){
			$data = $this->postData('data');
			$data['photo'] = arg('photo');

			$data['shifu_id'] = $this->admin['id'];
			// dump($data);exit;
			$result = $this->fankuiObj->order_act($order_id, $data);
			if($result){
				//log
				$this->fankui_logObj->add_log($order_id,$data);
				//发送短信给师傅
				// $this->smsObj->send_user_sms($order_id,$this->company_setting['sms'],'company');

				// $fk_id = $order_id;
				// $shifu = $this->admin_alls[$val];
				// // 师傅发送模版消息
				// $msg = "管理员重派了订单，请及时处理。";
				// $this->fankuiObj->send_weixin_msg($this->accessToken,$shifu,$fk_id,$msg);

				// if($shifu){
				// 	$admin_arr[] = $shifu['id'];
				// 	// 师傅发送模版消息
				// 	$msg = "存在不满意度选项，请及时维护。";
				// 	$this->fankuiObj->send_weixin_msg($this->accessToken,$shifu,$fk_id,$msg);
				// }

				$this->renderSuccess('操作成功','/'.$this->routeUrl.'/index?tab='.$data['order_status']);
			}
		}

		$act_arr = array(
    		'2'=>'重派反馈',
    		'3'=>'确认反馈',
    		'4'=>'维修反馈',
    		'5'=>'完成反馈',
    		'6'=>'评价反馈',
    		'7'=>'结算反馈',
    		'99'=>'取消反馈',
    	);
    	$this->act_name = $act_arr[$order_status];
    	$this->order_status = $order_status;

		$this->info = $this->fankuiObj->get_detail($order_id);

		$this->intro="您反馈的（{$this->info['title']}）问题我们已经收到，并尽快安排处理，感谢您的反馈。";

	}


	//评价
	public function pingjia(){
		$order_id = arg('id');
		if($this->fmdo == 'pingjia'){
			$data = $this->postData('data');

			$order_status = 6;
			$arrBase = array(
				'pj_star1' =>arg('pj_star1'),
				'pj_star2' =>arg('pj_star2'),
				'pj_info' =>arg('pj_info'),
				'order_status' =>$order_status,
				'up_time' =>date('Y-m-d H:i:s'),
				'status' =>1,
			);
			$result = $this->orderObj->edit($order_id, $arrBase);
			if($result){
				//log
				$data['order_status'] = $order_status;
				$this->order_logObj->add_log($order_id,$data);
				//发送短信给师傅
				// $this->smsObj->send_user_sms($order_id,$this->company_setting['sms'],'company');

				$this->renderSuccess('评价成功','/'.$this->routeUrl.'/index?tab='.$data['order_status']);
			}
		}

		$this->info = $this->orderObj->get_detail($order_id);
	}

	//查看评价
	public function pingjia_look(){
		$id = arg('id');
		$info = $this->orderObj->get_info($id);

		if($info['pj_star1']){
			for ($i=1; $i <= $info['pj_star1']; $i++) {
				$pj_star1[$i] = 'checked';
			}
			for ($i=1; $i <= $info['pj_star2']; $i++) {
				$pj_star2[$i] = 'checked';
			}
			$this->pj_star1 = $pj_star1;
			$this->pj_star2 = $pj_star2;
		}

		$this->info = $info;
	}


	//结算
	function jiesuan(){
		$id = arg('id');
		if($this->fmdo == 'jiesuan'){
			$order_status = 6;
			$arrBase = array(
				// 'yongjin'=>arg('yongjin'),
				// 'yj_money'=>arg('yj_money'),
				'order_status' =>$order_status,
				'up_time' =>date('Y-m-d H:i:s'),
				'status' =>1,
			);
			$this->modelObj->update(array('id'=>$id), $arrBase);

			$data = array(
				'msg'=>'佣金已结算',
				'intro'=>arg('intro'),
				'photo'=>joinStr(arg('photo')),
				'yongjin'=>arg('yongjin'),
				'yj_money'=>arg('yj_money'),
			);
			$this->order_logObj->add_log($order_id,$data);

			//发送短信
			D('sms')->send_web_sms($order_id);

			$this->ajaxMsg('信息结算成功',1);
		}

		$info = $this->modelObj->findById($id);
		// $info['yj_money'] = $info['jiage'] * 0.4;
		$this->info = $info;
	}



	public function ajax_child(){
		$data['type'] = arg('type');
		$data['id'] = arg('id');
		$data[$data['type'].'_ids'] = $this->admin[$data['type'].'_ids'];
		// dump($data);exit;
		$result = $this->fankuiObj->get_child($data);
		if($result){
			$this->info = $result['info'];
			$this->list = $result['list'];
			$this->type = $data['type'];
		}

	}

	//ajax_shifu_list
	function ajax_shifu_list()
	{
		$cate_id = arg('cate_id');
		if($cate_id){
			// 排班师傅
			$cate = D('company_cate')->findById($cate_id);
			$shifu_pai_ids = $cate['shifu_ids'];
			if($shifu_pai_ids){
				$cond = "status=1 and (level=3 or level=4) and is_sign=1 " .$this->cond_base;
				// $cond .= " and find_in_set('{$cate_id}',cate_ids)";
				$cond .= " and id IN ({$shifu_pai_ids})";
			}
			$shifus = $this->adminObj->findAll(array($cond),'id asc');
			if(!$shifus){
				$cond = "status=1 and (level=3 or level=4) and is_zhiban=1 " .$this->cond_base;
				$cond .= " and find_in_set('{$cate_id}',cate_ids)";
				$shifus = $this->adminObj->findAll(array($cond),'id asc');
			}

			$this->list = $shifus;
		}
	}


	public function ajax_shifu()
	{
		$data['area_id'] = arg('area_id');
		$data['cate_id'] = arg('cate_id');
		$data['zone_id'] = arg('zone_id');
		$data['company_id'] = $this->company['id'];

		$shifu_id = $this->adminObj->get_shifu_id($data,$this->config);
		// dump($this->config);exit;
		if($shifu_id){
			$info = $this->adminObj->findById($shifu_id);
			$msg = $info['realname'].' - '.$info['tel'];
			if($info['is_zhiban']){
				$msg .= ' (值班中)';
			}

			header('Content-Type:application/json; charset=utf-8');
			// $this->renderSuccess();
			$data = array();
			$data['code'] = 1;
			$data['shifu_id'] = $shifu_id;
			$data['is_zhiban'] = $info['is_zhiban'];
			$data['msg'] = $msg;
			exit(json_encode($data));
		}else{
			// echo '0';
			$data = array();
			$data['code'] = 0;
			$data['shifu_id'] = 0;
			$data['msg'] = '没有可分配师傅';
			exit(json_encode($data));
		}

	}

}